package com.packagetracker.views;

import java.util.LinkedList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ExpandableListActivity;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.Spinner;

import com.packagetracker.R;
import com.packagetracker.adapters.PackageListAdapter;
import com.packagetracker.entities.TrackingNumber;
import com.packagetracker.helpers.NumbersHelper;
import com.packagetracker.providers.TrackerProviderSetup;
import com.packagetracker.utils.AnimateUtil;
import com.packagetracker.utils.AppUtil;

public class TrackerNumbers extends ExpandableListActivity implements ExpandableListView.OnChildClickListener, AdapterView.OnItemSelectedListener {
	
	public static final int MENU_ADD_NEW = 0;
	public static final int MENU_ABOUT = 1;
	public static final int MENU_QUIT = 2;
	public static final int DIALOG_ADD = 0;
	public static final int DIALOG_ABOUT = 1;
	public static final int DIALOG_INFO = 2;
	
	@Override
	public void onCreate(final Bundle bundle) {
		super.onCreate(bundle);
		
		this.setContentView(R.layout.numbers_view);
		this.getExpandableListView().setLayoutAnimation(AnimateUtil.cascade());
		this.registerForContextMenu(this.getExpandableListView());
		
		TrackerNumbers.this.loadNumbers();
	}
	
	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		super.onCreateOptionsMenu(menu);
		
		menu.add(0, MENU_ADD_NEW, Menu.FIRST, R.string.add_label).setIcon(R.drawable.menu_add);
		menu.add(0, MENU_ABOUT, (Menu.FIRST + 1), R.string.about_label).setIcon(R.drawable.menu_about);
		menu.add(0, MENU_QUIT, (Menu.FIRST + 2), R.string.quit_label).setIcon(R.drawable.menu_quit);
		
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		super.onOptionsItemSelected(item);
		
		switch (item.getItemId()) {
			case MENU_ADD_NEW:
				this.showDialog(DIALOG_ADD);
				break;
				
			case MENU_ABOUT:
				this.showDialog(DIALOG_ABOUT);
				break;
				
			case MENU_QUIT:
				this.finish();
				break;
		}
		
		return true;
	}
	
	@Override
	public boolean onChildClick(final ExpandableListView parent, final View view, final int groupPosition, final int childPosition, final long childId) {
		final String trackingNumber = ((TrackingNumber)parent.getExpandableListAdapter().getGroup(groupPosition)).getNumber();
		
		switch (childPosition) {
			case 0:
				final Intent intent = new Intent(new Intent(TrackerNumbers.this, TrackerMap.class));
				intent.putExtra(TrackerProviderSetup.Numbers.NUMBER, trackingNumber);
				
				this.startActivity(intent);
				break;
				
			case 2:
				this.getContentResolver().delete(Uri.parse("content://" + TrackerProviderSetup.URI + "/" + TrackerProviderSetup.Numbers.TABLE_NAME + "/" + new NumbersHelper(this).getNumber(trackingNumber).getId()), null, null);
				this.loadNumbers();
				
				AppUtil.showToastLong(TrackerNumbers.this, R.string.editor_remove_confirm);
				break;
		}
		
		return true;
	}
	
	@Override
	protected Dialog onCreateDialog(final int dialog) {
		switch (dialog) {
			case DIALOG_ADD:
				final View view = LayoutInflater.from(this).inflate(R.layout.editor_dialog, null);
				
				final ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_spinner_item, this.getResources().getStringArray(R.array.carriers));
				adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				
				final Spinner spinner = (Spinner)view.findViewById(R.id.add_carrier_field);
				spinner.setAdapter(adapter);
				spinner.setOnItemSelectedListener(this);
				
				return(new AlertDialog.Builder(this)
					.setTitle(R.string.add_title)
					.setView(view)
					.setPositiveButton(R.string.ok_label, new DialogInterface.OnClickListener() {
						public void onClick(final DialogInterface dialog, final int button) {
							TrackerNumbers.this.setResult(TrackerMap.RESULT_OK);
							
							final ContentValues values = new ContentValues();
							values.put(TrackerProviderSetup.Numbers.CARRIER, spinner.getSelectedItemId());
							values.put(TrackerProviderSetup.Numbers.NUMBER, ((EditText)view.findViewById(R.id.add_number_field)).getText().toString());
							
							TrackerNumbers.this.getContentResolver().insert(TrackerProviderSetup.Numbers.NUMBERS_URI, values);
							TrackerNumbers.this.loadNumbers();
							
							AppUtil.showToastLong(TrackerNumbers.this, R.string.editor_add_confirm);
						}
					})
					.setNegativeButton(R.string.cancel_label, null)
	                .create());
					
			case DIALOG_ABOUT:
				return(new AlertDialog.Builder(this)
					.setTitle(R.string.about_title)
					.setMessage(R.string.about_msg)
					.setPositiveButton(R.string.ok_label, null)
					.setNegativeButton(R.string.homepage_label, new DialogInterface.OnClickListener() {
						public void onClick(final DialogInterface dialog, final int button) {
							TrackerNumbers.this.setResult(TrackerMap.RESULT_OK);
							TrackerNumbers.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(TrackerNumbers.this.getString(R.string.homepage_uri))));
						}
					})
					.create());
		}
		
		return null;
	}
	
	@Override
	public void onItemSelected(final AdapterView<?> parent, final View view, final int position, final long itemId) {}
	
	@Override
	public void onNothingSelected(final AdapterView<?> parent) {}
	
	private void loadNumbers() {
		final LinkedList<TrackingNumber> numbers = new NumbersHelper(this).getNumbers();
		if (numbers.size() > 0) {
			this.setListAdapter(new PackageListAdapter(this, numbers));
		}
	}
	
	public String[] getMenuOptions() {
		return new String[] {
				this.getString(R.string.map_label),
				this.getString((R.string.numbers_update)),
				this.getString((R.string.numbers_remove))
			};
	}
	
}